<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>阳光成单系统</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="layui-v2.6.4/layui/css/layui.css">
<script type="text/javascript" src="layui-v2.6.4/layui/layui.js"></script>
</head>
<body>
<div style="display: none"  id="tianjiaRead">
	<form id="formRead" lay-filter="huixian"   class="layui-form" onsubmit= "return false;">
		<input type="text" id="read_romm_id" name="read_romm_id" style="display: none;" />
	
		<div class="layui-input-inline">
			<label class="layui-form-label">资源室名称</label>
			<div class="layui-input-block">
				<input type="text" id="read_room_name"  name="read_room_name" 
					autocomplete="off" lay-verify="required" placeholder="请输入资源室名称" class="layui-input">
			</div>
		</div>
		<div class="layui-input-inline">
			<label class="layui-form-label">备注</label>
			<div class="layui-input-block">
				<input type="text" id="read_romm_remark" name="read_romm_remark" 
					autocomplete="off" lay-verify="required" placeholder="请输入标题资源室备注" class="layui-input">
			</div>
		</div>
		
			
		<div class="layui-form-item">
			<div class="layui-input-block" >
				<button type="submit" id="tianjia" class="layui-btn" lay-submit=""
					lay-filter="add_ReadRoom" >立即添加</button>
					<button type="submit" id="xiugai" class="layui-btn" lay-submit=""
					lay-filter="deletRead">立即修改</button>
			</div>
		</div>
	</form>
	
	
</div>
<form  lay-filter="jr" id="jr2"   class="layui-form" onsubmit= "return false;">
	<div id="jr" style="display: none;">
			<div class="layui-input-inline">
					<label class="layui-form-label">会员卡号</label>
					<div class="layui-input-block">
						<input type="text" id="myCard" autocomplete="off" lay-verify="required" placeholder="请输入会员卡号" class="layui-input">
					</div>
					
			</div>
			<div style="display: none;" class="layui-input-inline">
				<label class="layui-form-label">资源室名称</label>
				<div class="layui-input-block">
					<input type="text" id="read_room_id"  name="read_romm_id" 
						autocomplete="off" lay-verify="required" placeholder="请输入资源室名称" class="layui-input">
				</div>
			</div>
			
					<button type="submit" id="tianjia" class="layui-btn" lay-submit=""
						lay-filter="jinRu" >进入</button>
	</div>
</form>
	

		<script type="text/html" id="toolbarDemo">
 			<div class="layui-btn-container">
  		  		<button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
  			</div>
		</script>
	

	<table id="demo" lay-filter="test"></table>
	
	
	
	

	<script type="text/html" id="barDemo">
		<a class="layui-btn layui-btn-xs" lay-event="jinru">进入</a>
 	 	<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  
	</script>
		


	<script>
	var table, layer, form, $;
		layui.use(['table','layer','form'], function(){
		  table = layui.table;
		  layer=layui.layer;
		  form=layui.form;
		  $=layui.$;
		  
		  //第一个实例
		  table.render({
		    elem: '#demo'
		    ,url: 'getRoom' //数据接口
		    ,id:"myRead"
		    ,page: true //开启分页
		    ,toolbar : '#toolbarDemo'
		    ,cols: [[ //表头
		      {field: 'read_romm_id', title: '阅读室资源Id'}
		      ,{field: 'read_room_name', title: '阅读室资源名称'}
		      ,{field: 'read_romm_remark', title: '备注'}
		      ,{fixed: 'right', width:250, align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器
		    ]]
		  });
		  
	
		 table.on('toolbar(test)', function(obj){
		 		$("#formRead")[0].reset();	
		  		$("#tianjia").show();
		  		$("#xiugai").hide();
		  	layer.open({
				  type: 1, 
				  content:$("#tianjiaRead"),
				  area:['400px','400px']
				});
				
		  	})
		    
		 
		 form.on('submit(add_ReadRoom)', function(data){
						
				
		  		$.post("addRoom",
		  			$("#formRead").serialize()
		  		,function(res){
		  			if(res.code=="0"){
		  				layer.msg("添加成功")
		  					layer.closeAll();
						table.reload("myRead");
		  				
		  				} else{
		  					layer.msg("添加失败")
		  				
		  				}
		  				
		  		},"json")		
		  		return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		});
		 
		
	  		
	  		
		  
		  
		  
					  //工具条事件
			table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
			  var data = obj.data; //获得当前行数据
			  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
			 
			 
			  if(layEvent === 'del'){ //删除
			    layer.confirm('真的删除行么', function(index){
			    
			  	console.log(data);
			     $.post("deleteRooms",{
			     	read_romm_id:data.read_romm_id
			     },function(res){
			     	if(res.code=="0"){
			     		layer.msg("删除成功")
			     		table.reload("myRead");
			     	} else {
			     		layer.msg("删除失败")
			     	
			     	}
			     
			     },"json")
			    
			    
			   layer.close(index);
				//向服务端发送删除指令
			    });
			  } else if(layEvent === 'edit'){ //编辑
		
		 		form.val("huixian", data);
		 		$("#xiugai").show();
			 		$("#tianjia").show;
			 		$("#tianjia").hide();
			 	 layer.open({
				  type: 1, 
				  content:$("#tianjiaRead"),
				  area:['400px','400px']
				  
				});
			  } else if(layEvent === 'jinru'){ //进入
			 	 $("#jr2")[0].reset();
			  	form.val("jr", data);
					layer.open({
					  type: 1, 
					  content:$("#jr"),
					  area:['400px','400px']
					});
		
			  }
			  
			  
			});
		  
		  //修改
		   form.on('submit(deletRead)', function(data){
		  		$.post("updateRoom",
		  			$("#formRead").serialize()
		  		,function(res){
		  			if(res.code=="0"){
		  					layer.msg("修改成功")
		  					layer.closeAll();
						table.reload("myRead");
		  				
		  				} else{
		  					layer.msg("修改失败")
		  				
		  				}
		  				
		  		},"json")		
		  		return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		});
		
		//進入资源室
		   form.on('submit(jinRu)', function(data){
		  			$.post("addConsume_log",{
		  				card_id:$("#myCard").val(),
		  				read_room:$("#read_room_id").val(),
		  				mId:$("#myCard").val(),
				  		rId:$("#read_room_id").val()
		  			},function(res){
		  				if(res.code=="0"){
		  					layer.msg("加入成功");
		  					$.post("updStatus",{
				  				mId:$("#myCard").val(),
				  				rId:$("#read_room_id").val()
				  			},function(res){
				  				if(res.code=="0"){
				  					layer.msg("加入成功");
				  					
				  					layer.closeAll();
				  					
				  				}else{
				  					layer.alert("该会员不存在");
				  					
				  				}
				  			},"json");
		  					layer.closeAll();
		  				}else{
		  					layer.msg("失败");
		  				}
		  			},"json");	
		  		return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		});
		  
		  
		  
		});
</script>

</body>
</html>